<?php

class ClientsController extends Zend_Controller_Action
{
    function preDispatch()
    {
		if(!$_SESSION['identity'])
        {
            $this->_redirect("/login");
        }
		
         //Make menu
         $mod_menu = new Db_Tables2_BDictMenu();
         $this->view->main_menu = $mod_menu->MenuDecolation();
        
        $mod_params = new Db_Tables2_BDictParams();
        $this->view->sys_name = $mod_params->GetSysName();
    }
	
    function indexAction()
    {
		$params = $this->_request->getParams();
		
		$this->view->title = "客户列表";
		
		$mod_t_area = new Db_Tables2_TArea();
		$this->view->provinces = $mod_t_area->GetProvinces();
		
		$params_pool = array();
		$mod_tables = new Db_Joins_TablesJoins();
    	
    	if($params['provinceId'])
    	{
    		$mod_tables->provinceId = $params['provinceId'];
    		$this->view->provinceId = $params['provinceId'];
    		$params_pool[] = "provinceId=".$params['provinceId'];
			
			$this->view->init_cities = $mod_t_area->GetCities($params['provinceId']);
    	}
    	
    	if($params['cityId'])
    	{
    		$mod_tables->cityId = $params['cityId'];
    		$this->view->cityId = $params['cityId'];
    		$params_pool[] = "cityId=".$params['cityId'];
    	}
		
		if($params['status'] && !empty($params['status']))
    	{
			$status_str = implode("_", $params['status']);
    		$mod_tables->status_str = $status_str;
    		$this->view->status_str = $status_str;
    		$params_pool[] = "status_str=".$status_str;
			$this->view->status_str = $params['status'];
    	}elseif($params['status_str'])
		{
			$status = explode("_", $params['status_str']);
    		$mod_tables->status_str = $params['status_str'];
    		$this->view->status_str = $params['status_str'];
    		$params_pool[] = "status_str=".$params['status_str'];
			$this->view->status_arr = $status;
		}
		
		if($params['keyword'])
    	{
    		$mod_tables->keyword = $params['keyword'];
    		$this->view->keyword = $params['keyword'];
    		$params_pool[] = "keyword=".$params['keyword'];
    	}
		
		if(!$params['page_id'])
    	{
    		$page_id = 1;
    	}else{
    		$page_id = $params['page_id'];
    	}
    	
    	if($page_id)
    	{
    		$mod_tables->page_id = $page_id;
    		$this->view->page_id = $page_id;
    	}
		
		$this->view->data = $result = $mod_tables->GetClients();
		
		//make pagination
    	if(!empty($params_pool))
    	{
    		$params_string = implode("&", $params_pool);
    	}else{
    		$params_string = "";
    	}
    	
    	if(1 == $page_id)
    	{
    		$first_page_string = "";
    		$previous_page_string = "";
    	}else{
    		$first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
    		$previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
    	}
    	
    	if($page_id < $result['total_pages'] )
    	{
    		$next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
    		$last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
    	}else{
    		$next_page_string = "";
    		$last_page_string = "";
    	}
    	
		$this->view->params_string = $params_string;
    	$this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;
		
		if($params['msg'])
		{
				switch ($params['msg'])
				{
					case 1:
						$this->view->msg = "您已成功添加客户，请等待审核。审核通过前，您无法在合同中选择此客户。";
						break;
				}
		}
	}
	
	function addClientAction()
	{
		$params = $this->_request->getParams();
		$this->view->title = "申请客户";
		
		$mod_t_area = new Db_Tables2_TArea();
		$this->view->provinces = $mod_t_area->GetProvinces();
		
		$mod_client_type = new Db_Tables2_BClientType();
		$this->view->client_types = $mod_client_type->GetTypes();
		
		$mod_factory = new Db_Tables2_TFactory();
		$this->view->factories = $mod_factory->GetFactories();
		
		$mod_t_signer = new Db_Tables2_BSigner();
		$this->view->signers = $mod_t_signer->GetSigners();
		
		if($params['error'])
		{
				switch ($params['error'])
				{
					case 1:
						$this->view->msg = "关键参数缺失";
						break;
					case 2:
						$this->view->msg = "写入数据失败";
						break;
					default:
						$this->view->msg = "其他错误";
						break;
				}
		}
	}
	
	function addClientSubmitAction()
	{
		$params = $this->_request->getParams();
		
		if($params['client_name'] && $params['provinceId'] && $params['cityId'] && $params['client_type_id'])
		{
			$mod_dealer = new Db_Tables2_TDealer();
			$is_dealer = $mod_dealer->SearchDealerByKeyword($params['client_name'], 1);
			
			$mod_t_area = new Db_Tables2_TArea();
			$region = $mod_t_area->GetNames($params['provinceId'], $params['cityId'], $params['countyId']);
			
			$data = array(
				"client_name" => trim($params['client_name']),
				"province_id" => $region['provinceId'],
				"province_name" => $region['provinceName'],
				"city_id" => $region['cityId'],
				"city_name" => $region['cityName'],
				"county_id" => $region['countyId'],
				"county_name" => $region['countyName'],
				"address" => trim($params['address']),
				"zipcode" => trim($params['zipcode']),
				"tel" => trim($params['tel']),
				"cellphone" => trim($params['cellphone']),
				"fax" => trim($params['fax']),
				"client_type_id" => trim($params['client_type_id']),
				"emp_id" => $_SESSION['identity']['userid'],
				"ctime" => date("Y-m-d H:i:s"),
				"status" => 0
			);
			
			if($is_dealer)
			{
				$data['is_dealer'] = $is_dealer;
			}
			
			$dealers_db = Zend_Registry::get('dealers_db');
			if($dealers_db->insert("b_client", $data))
			{
				$client_id = 0;
				
				$select = $dealers_db->select();
				$select->from("b_client", "client_id");
				$select->order("client_id DESC");
				$row = $dealers_db->fetchRow($select);

				if($row)
				{
					$client_id = $row['client_id'];
				}
				
				if($client_id)
				{
					//添加主管审批记录
					$mod_user = new Db_Tables2_User();
					$leaders_arr = $mod_user->GetMyLeaders($_SESSION['identity']['userid']);
					
					if(!empty($leaders_arr))
					{
//						foreach($leaders_arr as $leader)
//						{
//							$set = array(
//								"type" => 1,
//								"target_id" => $client_id,
//								"emp_id" => $leader['emp_id'],
//								"emp_name" => $leader['emp_name'],
//								"status" => 0
//							);
//							
//							$dealers_db->insert("b_logs_sales_approval", $set);
//							break; //只要直接上级
//						}
						
						//等待合同管理员审核 2019.8.7 Mark
						$set = array("status" => 1);
					}else{
						//直接通过
						$set = array("status" => 2);
					}
					
					$where = "client_id=".$client_id;
					$dealers_db->update("b_client", $set, $where);
					
					//添加其他关联关系
					if($params['factory_id'])
					{
						$set = array(
							"client_id" => $client_id,
							"factory_id" => $params['factory_id']
						);

						$dealers_db->insert("b_client_factory", $set);
					}
					
					if($params['signer_id'])
					{
						$set = array(
							"client_id" => $client_id,
							"signer_id" => $params['signer_id'],
							"status" => 1 //自动通过审核
						);

						$dealers_db->insert("b_client_signer", $set);
					}
				}
				
				if($params['signer_id'])
				{
					$this->_redirect("/clients/index/msg/1");
				}else{
					$_SESSION['tmp_client_info'] = $params;
					
					$this->_redirect("/clients/add-signer");
				}
			}else{
				$this->_redirect("/clients/add-client/error/2"); //关键参数缺失
			}
		}else{
			$this->_redirect("/clients/add-client/error/1"); //关键参数缺失
		}
	}
	
	function editClientAction()
	{
		$params = $this->_request->getParams();
		
		if($params['client_id'])
		{
			$dealers_db = Zend_Registry::get('dealers_db');
			$select = $dealers_db->select();
			$select->from("b_client as c", array("client_id", "client_name", "province_id", "province_name", "city_id", "city_name", "county_id", "county_name", "address", "zipcode", "tel", "cellphone", "fax", "client_type_id", "is_dealer", "emp_id", "emp_id_3", "status", "ctime"));
			$select->joinLeft("t_dealer_factory as f", "f.dealerId=c.is_dealer", array("factoryId as factory_id"));
			$select->where("c.client_id = ?", $params['client_id']);
			
			if("LEVEL-1" == $_SESSION['identity']['level']) //销售只能改自己
			{
				$select->where("c.emp_id = ".$_SESSION['identity']['userid']." or c.emp_id is null");
			}
			
			$row = $dealers_db->fetchRow($select);
			
			if($row)
			{
				$this->view->title = "编辑".$row['client_name'];
				$this->view->data = $row;
				$mod_t_area = new Db_Tables2_TArea();
				$this->view->init_cities = $mod_t_area->GetCities($row['province_id']);
				$mod_t_region = new Db_Tables2_TRegion();
				$this->view->init_counties = $mod_t_region->GetCounties($row['city_id']);
				
				//获取签约公司
				$select = $dealers_db->select();
				$select->from("b_client_signer as c", array("id as row_id", "status"));
				$select->joinLeft("b_signer as s", "s.signer_id=c.signer_id", array("company_name"));
				$select->where("client_id = ?", $params['client_id']);
				$this->view->been_signers = $dealers_db->fetchAll($select);

				$mod_factory = new Db_Tables2_TFactory();
				$this->view->factories = $mod_factory->GetFactories();
				
				$mod_params = new Db_Tables2_BDictParams();
				$sales_role_id = array();
				$sales_role_id[] = $mod_params->GetVal("LEVEL-1");
				$sales_role_id[] = $mod_params->GetVal("LEVEL-2");

				$mod_user = new Db_Tables2_User();
				$this->view->sales = $mod_user->GetUsersByRole($sales_role_id);
				
				//合同管理员 获取所有dealer_id
				$mod = new Db_Tables2_TDealer();
				$this->view->dealers_in_same_name = $mod->GetDealerInSameName($row['client_name']);
			}else{
				$this->_redirect("/clients/index/error/3");
			}
		}else{
			$this->_redirect("/clients/index/error/1");
		}
		
		$mod_t_area = new Db_Tables2_TArea();
		$this->view->provinces = $mod_t_area->GetProvinces();
		
		$mod_client_type = new Db_Tables2_BClientType();
		$this->view->client_types = $mod_client_type->GetTypes();
		
		$mod_t_signer = new Db_Tables2_BSigner();
		$this->view->signers = $mod_t_signer->GetSigners();
			
		if($params['msg'])
		{
			switch ($params['msg'])
			{
				case 1:
					$this->view->msg = "客户信息修改成功。<a href='/clients/index'>【返回客户列表】</a>";
					break;
			}
		}
	}
	
	function editClientSubmitAction()
	{
		$params = $this->_request->getParams();
		
		if($params['client_id'] && $params['client_name'] && $params['provinceId'] && $params['cityId'] && $params['client_type_id'])
		{
			$mod_t_area = new Db_Tables2_TArea();
			$region = $mod_t_area->GetNames($params['provinceId'], $params['cityId'], $params['countyId']);
			
			$data = array(
				"client_name" => trim($params['client_name']),
				"province_id" => $region['provinceId'],
				"province_name" => $region['provinceName'],
				"city_id" => $region['cityId'],
				"city_name" => $region['cityName'],
				"county_id" => $region['countyId'],
				"county_name" => $region['countyName'],
				"address" => trim($params['address']),
				"zipcode" => trim($params['zipcode']),
				"tel" => trim($params['tel']),
				"cellphone" => trim($params['cellphone']),
				"fax" => trim($params['fax']),
				"client_type_id" => trim($params['client_type_id'])
			);
			
			if($params['emp_id'])
			{
				$data['emp_id'] = $params['emp_id'];
			}
			
			if($params['is_dealer'])
			{
				$data['is_dealer'] = $params['is_dealer'];
			}
			
			$dealers_db = Zend_Registry::get('dealers_db');
			$where = "client_id = ".$params['client_id'];
			$dealers_db->update("b_client", $data, $where);
			
			if($params['signer_id'] && "create" != $params['signer_id']) //新增签约公司
			{
				$select = $dealers_db->select();
				$select->from("b_client_signer", array("id"));
				$select->where("client_id = ?", $params['client_id']);
				$select->where("signer_id = ?", $params['signer_id']);
				$row = $dealers_db->fetchRow($select);
				
				if(!$row)
				{
					$data = array(
						"client_id" => $params['client_id'],
						"signer_id" => $params['signer_id'],
						"status" => 1 //自动通过
					);
					
					$dealers_db->insert("b_client_signer", $data);
				}
			}elseif("create" == $params['signer_id'])
			{
				$_SESSION['tmp_client_info'] = $params;
				
				$this->_redirect("/clients/add-signer");
			}
			
			$this->_redirect("/clients/edit-client/client_id/".$params['client_id']."/msg/1");
		}else{
			$this->_redirect("/clients/index/error/1");
		}
	}
	
	function delSignerAction()
	{
		$params = $this->_request->getParams();
		
		if($params['row_id'] && $params['client_id'])
		{
			$dealers_db = Zend_Registry::get('dealers_db');
			$where = "id=".$params['row_id'];
			$dealers_db->delete("b_client_signer", $where);
			
			$this->_redirect("/clients/edit-client/client_id/".$params['client_id']."/msg/1");
		}else{
			echo "非法操作";
			die;
		}
	}
	
    function dealersAction()
    {
		$params = $this->_request->getParams();
		
		$this->view->title = "经销商列表";
		
		$mod_t_area = new Db_Tables2_TArea();
		$this->view->provinces = $mod_t_area->GetProvinces();
		
		$params_pool = array();
		$mod_tables = new Db_Joins_TablesJoins();
    	
    	if($params['provinceId'])
    	{
    		$mod_tables->provinceId = $params['provinceId'];
    		$this->view->provinceId = $params['provinceId'];
    		$params_pool[] = "provinceId=".$params['provinceId'];
			
			$this->view->init_cities = $mod_t_area->GetCities($params['provinceId']);
    	}
    	
    	if($params['cityId'])
    	{
    		$mod_tables->cityId = $params['cityId'];
    		$this->view->cityId = $params['cityId'];
    		$params_pool[] = "cityId=".$params['cityId'];
    	}
		
		if($params['authState'] && !empty($params['authState']))
    	{
			$authState_str = implode("_", $params['authState']);
    		$mod_tables->authState_str = $authState_str;
    		$this->view->authState_str = $authState_str;
    		$params_pool[] = "authState_str=".$authState_str;
			$this->view->authState_arr = $params['authState'];
    	}elseif($params['authState_str'])
		{
			$authState = explode("_", $params['authState_str']);
    		$mod_tables->authState_str = $params['authState_str'];
    		$this->view->authState_str = $params['authState_str'];
    		$params_pool[] = "authState_str=".$params['authState_str'];
			$this->view->authState_arr = $authState;
		}
		
		if($params['keyword'])
    	{
    		$mod_tables->keyword = $params['keyword'];
    		$this->view->keyword = $params['keyword'];
    		$params_pool[] = "keyword=".$params['keyword'];
    	}
		
		if(!$params['source_type'])
    	{
    		$source_type = 1;
    	}else{
    		$source_type = $params['source_type'];
    	}
		
		$mod_tables->source_type = $source_type;
    	$this->view->source_type = $source_type;
    	$params_pool[] = "source_type=".$source_type;
		
		if(!$params['page_id'])
    	{
    		$page_id = 1;
    	}else{
    		$page_id = $params['page_id'];
    	}
    	
    	if($page_id)
    	{
    		$mod_tables->page_id = $page_id;
    		$this->view->page_id = $page_id;
    	}
		
		$this->view->data = $result = $mod_tables->GetDealers();
		
		//make pagination
    	if(!empty($params_pool))
    	{
    		$params_string = implode("&", $params_pool);
    	}else{
    		$params_string = "";
    	}
    	
    	if(1 == $page_id)
    	{
    		$first_page_string = "";
    		$previous_page_string = "";
    	}else{
    		$first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
    		$previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
    	}
    	
    	if($page_id < $result['total_pages'] )
    	{
    		$next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
    		$last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
    	}else{
    		$next_page_string = "";
    		$last_page_string = "";
    	}
    	
		$this->view->params_string = $params_string;
    	$this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;
		
		if($params['status'])
		{
				switch ($params['status'])
				{
					case 1:
						$this->view->msg = "您已成功添加经销商，请等待审核。审核通过前，您无法在合同中选择此经销商。";
						break;
				}
		}
	}
	
	function addDealerAction()
	{
		$params = $this->_request->getParams();
		$this->view->title = "新增经销商";
		
		$mod_t_area = new Db_Tables2_TArea();
		$this->view->provinces = $mod_t_area->GetProvinces();
		
		if($params['error'])
		{
				switch ($params['error'])
				{
					case 1:
						$this->view->msg = "关键参数缺失";
						break;
					case 2:
						$this->view->msg = "写入数据失败";
						break;
					default:
						$this->view->msg = "其他错误";
						break;
				}
		}
	}
	
	function addDealerSubmitAction()
	{
		$params = $this->_request->getParams();
		
		if($params['name'] && $params['companyName'] && $params['tel'] && $params['provinceId'] && $params['cityId'] && $params['type'])
		{
			$mod_t_area = new Db_Tables2_TArea();
			$region = $mod_t_area->GetNames($params['provinceId'], $params['cityId']);
			
			$data = array(
				"name" => trim($params['name']),
				"shopName" => trim($params['name']),
				"companyName" => trim($params['companyName']),
				"provinceId" => $region['provinceId'],
				"provinceName" => $region['provinceName'],
				"cityId" => $region['cityId'],
				"cityName" => $region['cityName'],
				"address" => trim($params['address']),
				"tel" => trim($params['tel']),
				"type" => trim($params['type']),
				"state" => 1,
				"updateTime" => date("Y-m-d H:i:s"),
				"authState" => 2 //待审核
			);
			
			$dealers_db = Zend_Registry::get('dealers_db');
			if($dealers_db->insert("t_dealer_hxb", $data))
			{
				$this->_redirect("/clients/dealers/status/1");
			}else{
				$this->_redirect("/clients/add-dealer/error/2"); //关键参数缺失
			}
		}else{
			$this->_redirect("/clients/add-dealer/error/1"); //关键参数缺失
		}
	}
	
    function signersAction()
    {
		$params = $this->_request->getParams();
		
		$this->view->title = "签约公司列表";
		
		$mod_t_area = new Db_Tables2_TArea();
		$this->view->provinces = $mod_t_area->GetProvinces();
		
		$params_pool = array();
		$mod_tables = new Db_Joins_TablesJoins();
    	
    	if($params['provinceId'])
    	{
    		$mod_tables->provinceId = $params['provinceId'];
    		$this->view->provinceId = $params['provinceId'];
    		$params_pool[] = "provinceId=".$params['provinceId'];
			
			$this->view->init_cities = $mod_t_area->GetCities($params['provinceId']);
    	}
    	
    	if($params['cityId'])
    	{
    		$mod_tables->cityId = $params['cityId'];
    		$this->view->cityId = $params['cityId'];
    		$params_pool[] = "cityId=".$params['cityId'];
    	}
		
		if($params['status'] && !empty($params['status']))
    	{
			$status_str = implode("_", $params['status']);
    		$mod_tables->status_str = $status_str;
    		$this->view->status_str = $status_str;
    		$params_pool[] = "status_str=".$status_str;
			$this->view->status = $params['status'];
    	}elseif($params['status_str'])
		{
			$status = explode("_", $params['status_str']);
    		$mod_tables->status_str = $params['status_str'];
    		$this->view->status_str = $params['status_str'];
    		$params_pool[] = "status_str=".$params['status_str'];
			$this->view->status = $status;
		}
		
		if($params['keyword'])
    	{
    		$mod_tables->keyword = $params['keyword'];
    		$this->view->keyword = $params['keyword'];
    		$params_pool[] = "keyword=".$params['keyword'];
    	}
		
		if(!$params['page_id'])
    	{
    		$page_id = 1;
    	}else{
    		$page_id = $params['page_id'];
    	}
    	
    	if($page_id)
    	{
    		$mod_tables->page_id = $page_id;
    		$this->view->page_id = $page_id;
    	}
		
		$this->view->data = $result = $mod_tables->GetSigners();
		
		//make pagination
    	if(!empty($params_pool))
    	{
    		$params_string = implode("&", $params_pool);
    	}else{
    		$params_string = "";
    	}
    	
    	if(1 == $page_id)
    	{
    		$first_page_string = "";
    		$previous_page_string = "";
    	}else{
    		$first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
    		$previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
    	}
    	
    	if($page_id < $result['total_pages'] )
    	{
    		$next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
    		$last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
    	}else{
    		$next_page_string = "";
    		$last_page_string = "";
    	}
    	
		$this->view->params_string = $params_string;
    	$this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;
		
		if($params['status'])
		{
				switch ($params['status'])
				{
					case 1:
						$this->view->msg = "您已成功添加签约公司。";
						break;
					case 2:
						$this->view->msg = "状态更改成功";
						break;
					case 3:
						$this->view->msg = "状态更改失败";
						break;
					case 4:
						$this->view->msg = "更改成功";
						break;
				}
		}
	}
	
	function addSignerAction()
	{
		$params = $this->_request->getParams();
		$this->view->title = "新增签约公司";
		
		$mod_t_area = new Db_Tables2_TArea();
		$this->view->provinces = $mod_t_area->GetProvinces();
		
		if($params['error'])
		{
				switch ($params['error'])
				{
					case 1:
						$this->view->msg = "关键参数缺失";
						break;
					case 2:
						$this->view->msg = "写入数据失败";
						break;
					default:
						$this->view->msg = "其他错误";
						break;
				}
		}
	}
	
	function addSignerSubmitAction()
	{
		$params = $this->_request->getParams();
		
		if(isset($_SESSION['tmp_client_info']))
		{
			unset($_SESSION['tmp_client_info']);
		}
		
		if($params['company_name'] && $params['province_id'] && $params['city_id'])
		{
			$mod_t_area = new Db_Tables2_TArea();
			$region = $mod_t_area->GetNames($params['province_id'], $params['city_id']);
			
			$data = array(
				"company_name" => trim($params['company_name']),
				"province_id" => $region['provinceId'],
				"province_name" => $region['provinceName'],
				"city_id" => $region['cityId'],
				"city_name" => $region['cityName'],
				"register_address" => trim($params['register_address']),
				"contact_name" => trim($params['contact_name']),
				"contact_address" => trim($params['contact_address']),
				"zipcode" => trim($params['zipcode']),
				"tel" => trim($params['tel']),
				"fax" => trim($params['fax']),
				"company_owner" => trim($params['company_owner']),
				"business_number" => trim($params['business_number']),
				"tax_number" => trim($params['tax_number']),
				"bank_name" => trim($params['bank_name']),
				"bank_number" => trim($params['bank_number']),
				"is_regular_taxpayer" => $params['is_regular_taxpayer']?1:0,
				"status" => 1, //直接通过
				"emp_id" => $_SESSION['identity']['userid'],
				"ctime" => date("Y-m-d H:i:s")
			);
			
			$business_expired_date = trim($params['business_expired_date']);
			
			if($business_expired_date)
			{
				$data['business_expired_date'] = $business_expired_date;
			}
			
			$dealers_db = Zend_Registry::get('dealers_db');
			$signer_id = 0;
			
			if($dealers_db->insert("b_signer", $data))
			{
				$select = $dealers_db->select();
				$select->from("b_signer", "signer_id");
				$select->order("signer_id DESC");
				$row = $dealers_db->fetchRow($select);
				
				if($row)
				{
					$signer_id = $row['signer_id'];
				}
			}
			
			if($signer_id)
			{
				//upload image
				$set = array();
				
				if($_FILES['img1'])
				{
					$image = "img1-".$signer_id;

					if('image/jpeg' == $_FILES['img1']['type'])
					{
						$file_name = $image.".jpg";
					}elseif('image/png' == $_FILES['img1']['type'])
					{
						$file_name = $image.".png";
					}else{
						$file_name = NULL;
					}

					$file_folder = "upload/signers/";

					if($file_name)
					{
						move_uploaded_file($_FILES['img1']['tmp_name'], $file_folder.$file_name);
						$set['img1'] = $file_name;
					}
				}
				
				if($_FILES['img2'])
				{
					$image = "img2-".$signer_id;

					if('image/jpeg' == $_FILES['img2']['type'])
					{
						$file_name = $image.".jpg";
					}elseif('image/png' == $_FILES['img2']['type'])
					{
						$file_name = $image.".png";
					}else{
						$file_name = NULL;
					}

					$file_folder = "upload/signers/";

					if($file_name)
					{
						move_uploaded_file($_FILES['img2']['tmp_name'], $file_folder.$file_name);
						$set['img2'] = $file_name;
					}
				}
				
				if($_FILES['img3'])
				{
					$image = "img3-".$signer_id;

					if('image/jpeg' == $_FILES['img3']['type'])
					{
						$file_name = $image.".jpg";
					}elseif('image/png' == $_FILES['img3']['type'])
					{
						$file_name = $image.".png";
					}else{
						$file_name = NULL;
					}

					$file_folder = "upload/signers/";

					if($file_name)
					{
						move_uploaded_file($_FILES['img3']['tmp_name'], $file_folder.$file_name);
						$set['img3'] = $file_name;
					}
				}
				
				if($_FILES['img4'])
				{
					$image = "img4-".$signer_id;

					if('image/jpeg' == $_FILES['img4']['type'])
					{
						$file_name = $image.".jpg";
					}elseif('image/png' == $_FILES['img4']['type'])
					{
						$file_name = $image.".png";
					}else{
						$file_name = NULL;
					}

					$file_folder = "upload/signers/";

					if($file_name)
					{
						move_uploaded_file($_FILES['img4']['tmp_name'], $file_folder.$file_name);
						$set['img4'] = $file_name;
					}
				}
				
				if(!empty($set))
				{
					$where = "signer_id=".$signer_id;
					$dealers_db->update("b_signer", $set, $where);
				}
				
				$this->_redirect("/clients/signers/status/1");
			}else{
				$this->_redirect("/clients/add-signer/error/2"); //关键参数缺失
			}
		}else{
			$this->_redirect("/clients/add-signer/error/1"); //关键参数缺失
		}
	}
	
	function editSignerAction()
	{
		$params = $this->_request->getParams();
		
		if($params['signer_id'])
		{
			$dealers_db = Zend_Registry::get('dealers_db');
			$select = $dealers_db->select();
			$select->from("b_signer", "*");
			$select->where("signer_id = ?", $params['signer_id']);
			
			$row = $dealers_db->fetchRow($select);
			
			if($row)
			{
				$this->view->title = "编辑".$row['company_name'];
				$this->view->data = $row;
				$mod_t_area = new Db_Tables2_TArea();
				$this->view->init_cities = $mod_t_area->GetCities($row['province_id']);
				
				$mod_params = new Db_Tables2_BDictParams();
				$sales_role_id = $mod_params->GetVal("LEVEL-1");

				$mod_user = new Db_Tables2_User();
				$this->view->sales = $mod_user->GetUsersByRole($sales_role_id);
			}else{
				$this->_redirect("/clients/signers/error/3");
			}
		}else{
			$this->_redirect("/clients/signers/error/1");
		}
		
		$mod_t_area = new Db_Tables2_TArea();
		$this->view->provinces = $mod_t_area->GetProvinces();
			
		if($params['msg'])
		{
			switch ($params['msg'])
			{
				case 1:
					$this->view->msg = "签约公司信息修改成功。<a href='/clients/signers'>【返回签约公司列表】</a>";
					break;
			}
		}
	}
	
	function editSignerSubmitAction()
	{
		$params = $this->_request->getParams();
		
		if($params['company_name'] && $params['province_id'] && $params['city_id'])
		{
			$mod_t_area = new Db_Tables2_TArea();
			$region = $mod_t_area->GetNames($params['province_id'], $params['city_id']);
			
			$set = array(
				"company_name" => trim($params['company_name']),
				"province_id" => $region['provinceId'],
				"province_name" => $region['provinceName'],
				"city_id" => $region['cityId'],
				"city_name" => $region['cityName'],
				"register_address" => trim($params['register_address']),
				"contact_name" => trim($params['contact_name']),
				"contact_address" => trim($params['contact_address']),
				"zipcode" => trim($params['zipcode']),
				"tel" => trim($params['tel']),
				"fax" => trim($params['fax']),
				"company_owner" => trim($params['company_owner']),
				"business_number" => trim($params['business_number']),
				"tax_number" => trim($params['tax_number']),
				"bank_name" => trim($params['bank_name']),
				"bank_number" => trim($params['bank_number']),
				"business_expired_date" => trim($params['business_expired_date']),
				"is_regular_taxpayer" => $params['is_regular_taxpayer']?1:0,
				"emp_id" => $params['emp_id'],
				"status" => $params['status']
			);
			
			if($params['signer_id'])
			{
				$signer_id=  $params['signer_id'];
				
				//upload image
				if($_FILES['img1'])
				{
					$image = "img1-".$signer_id;

					if('image/jpeg' == $_FILES['img1']['type'])
					{
						$file_name = $image.".jpg";
					}elseif('image/png' == $_FILES['img1']['type'])
					{
						$file_name = $image.".png";
					}else{
						$file_name = NULL;
					}

					$file_folder = "upload/signers/";

					if($file_name)
					{
						move_uploaded_file($_FILES['img1']['tmp_name'], $file_folder.$file_name);
						$set['img1'] = $file_name;
					}
				}
				
				if($_FILES['img2'])
				{
					$image = "img2-".$signer_id;

					if('image/jpeg' == $_FILES['img2']['type'])
					{
						$file_name = $image.".jpg";
					}elseif('image/png' == $_FILES['img2']['type'])
					{
						$file_name = $image.".png";
					}else{
						$file_name = NULL;
					}

					$file_folder = "upload/signers/";

					if($file_name)
					{
						move_uploaded_file($_FILES['img2']['tmp_name'], $file_folder.$file_name);
						$set['img2'] = $file_name;
					}
				}
				
				if($_FILES['img3'])
				{
					$image = "img3-".$signer_id;

					if('image/jpeg' == $_FILES['img3']['type'])
					{
						$file_name = $image.".jpg";
					}elseif('image/png' == $_FILES['img3']['type'])
					{
						$file_name = $image.".png";
					}else{
						$file_name = NULL;
					}

					$file_folder = "upload/signers/";

					if($file_name)
					{
						move_uploaded_file($_FILES['img3']['tmp_name'], $file_folder.$file_name);
						$set['img3'] = $file_name;
					}
				}
				
				if($_FILES['img4'])
				{
					$image = "img4-".$signer_id;

					if('image/jpeg' == $_FILES['img4']['type'])
					{
						$file_name = $image.".jpg";
					}elseif('image/png' == $_FILES['img4']['type'])
					{
						$file_name = $image.".png";
					}else{
						$file_name = NULL;
					}

					$file_folder = "upload/signers/";

					if($file_name)
					{
						move_uploaded_file($_FILES['img4']['tmp_name'], $file_folder.$file_name);
						$set['img4'] = $file_name;
					}
				}
				
				$where = "signer_id=".$signer_id;

				$dealers_db = Zend_Registry::get('dealers_db');

				$dealers_db->update("b_signer", $set, $where);
				
				$this->_redirect("/clients/signers/status/4");
			}else{
				$this->_redirect("/clients/edit-signer/error/2"); //关键参数缺失
			}
		}else{
			$this->_redirect("/clients/edit-signer/error/1"); //关键参数缺失
		}
	}
	
	function showimgAction()
	{
		$this->_helper->layout->disableLayout();
		$params = $this->_request->getParams();
		
		$result = "";
		
		$path = "/upload/signers/";
		$dealers_db = Zend_Registry::get('dealers_db');
		
		$select = $dealers_db->select();
		$select->from("b_signer", "*");
		$select->where("signer_id = ?", $params['id']);
		$row = $dealers_db->fetchRow($select);
		
		if($row && $params['type'] && $params['id'])
		{
			switch($params['type'])
			{
				case 1:
					$result = $path.$row['img1'];
					break;
				case 2:
					$result = $path.$row['img2'];
					break;
				case 3:
					$result = $path.$row['img3'];
					break;
				case 4:
					$result = $path.$row['img4'];
					break;
			}
		}
		
		$this->view->result = $result;
	}
	
    function ownersAction()
    {
		$params = $this->_request->getParams();
		
		$this->view->title = "乙方单位列表";
		
		$mod_tables = new Db_Joins_TablesJoins();
    	
    	
		$this->view->data = $mod_tables->GetOwners();
		
		if($params['status'])
		{
				switch ($params['status'])
				{
					case 1:
						$this->view->msg = "您已成功添加乙方单位";
						break;
					case 2:
						$this->view->msg = "状态更改成功";
						break;
					case 3:
						$this->view->msg = "状态更改失败";
						break;
				}
		}
	}
	
	function addOwnerAction()
	{
		if("LEVEL-3" == $_SESSION['identity']['level'])
		{
			$params = $this->_request->getParams();
			$this->view->title = "新增乙方单位";

			if($params['error'])
			{
					switch ($params['error'])
					{
						case 1:
							$this->view->msg = "关键参数缺失";
							break;
						case 2:
							$this->view->msg = "写入数据失败";
							break;
						default:
							$this->view->msg = "其他错误";
							break;
					}
			}
		}else{
			echo "无此权限";
			die;
		}
	}
	
	function addOwnerSubmitAction()
	{
		$params = $this->_request->getParams();
		
		if($params['owner_name'] && "LEVEL-3" == $_SESSION['identity']['level'])
		{
			$data = array(
				"owner_name" => trim($params['owner_name']),
				"status" => 1,
				"ctime" => date("Y-m-d H:i:s")
			);
			
			$dealers_db = Zend_Registry::get('dealers_db');
			$signer_id = 0;
			
			if($dealers_db->insert("b_owner", $data))
			{
				$this->_redirect("/clients/owners/status/1");
			}else{
				$this->_redirect("/clients/add-owner/error/2"); //关键参数缺失
			}
		}else{
			$this->_redirect("/clients/add-owner/error/1"); //关键参数缺失
		}
	}
	
    function skuMgtAction()
    {
		$params = $this->_request->getParams();
		
		$this->view->title = "产品管理";
		
		$mod_products = new Db_Tables2_BDictProduct();
		
		$this->view->products = $mod_products->GetProducts();
	}
	
    function skuDetailsAction()
    {
		$params = $this->_request->getParams();
		$this->view->title = "SKU管理";
		
		$mod_b_sku = new Db_Tables2_BSku();
		
		if($params['product_id'])
		{
			$this->view->data = $mod_b_sku->GetSkuInfo($params['product_id'], 1, 1);
			$this->view->id = intval($params['product_id']);
			$this->view->id_type = 1;
			
			$mod_products = new Db_Tables2_BDictProduct();
			$product = $mod_products->FetchProductDetails($params['product_id']);
			$this->view->title = $product['product_name'];
		}elseif($params['sku_id'])
		{
			$this->view->data = $mod_b_sku->GetSkuInfo($params['sku_id'], 2, 1);
			$this->view->id = intval($params['sku_id']);
			$this->view->id_type = 2;
			
			$sku_path = $mod_b_sku->GetSkuPath($params['sku_id']);
			$this->view->title = $sku_path['path_name'];
		}else{
			echo "非法操作";
			die;
		}
		
		if($params['msg'])
		{
				switch ($params['msg'])
				{
					case 1:
						$this->view->msg = "更新成功。";
						break;
				}
		}
	}
	
	function skuDetailsSubmitAction()
	{
		$params = $this->_request->getParams();
//		$ext = new Sys_Extensions();
//		$ext->FormatArray($params);die;
		if($params['id'] && $params['id_type'])
		{
			$dealers_db = Zend_Registry::get('dealers_db');
			
			//existed
			foreach($params as $pkey => $pval)
			{
				if("o_sku_name" == substr($pkey, 0, 10))
				{
					$exp = explode("_", $pkey);
					$sku_id = $exp[3];
					
					$set = array();
					$set['sku_name'] = $params["o_sku_name_".$sku_id];
					
					if($params["o_unit_name_".$sku_id])
					{
						$set['unit_name'] = $params["o_unit_name_".$sku_id];
					}else{
						$set['unit_name'] = NULL;
					}
					
					if($params["o_unit_price_".$sku_id])
					{
						$set['unit_price'] = $params["o_unit_price_".$sku_id];
					}else{
						$set['unit_price'] = NULL;
					}
					
					$set['status'] = $params["o_status_".$sku_id];
					
					$where = "sku_id=".$sku_id;
					
					$dealers_db->update("b_sku", $set, $where);
				}
			}
			
			//new
			for($n=0;$n<5;$n++)
			{
				if($params['sku_name_'.$n])
				{
					$set['sku_name'] = $params['sku_name_'.$n];
					$set['status'] = $params['status_'.$n];
					
					if($params['unit_name_'.$n])
					{
						$set['unit_name'] = $params['unit_name_'.$n];
					}
					
					if(is_numeric($params['unit_price_'.$n]))
					{
						$set['unit_price'] = $params['unit_price_'.$n];
					}
					
					if(1 == $params['id_type'])
					{
						$set["product_id"] = $params['id'];
					}else{
						$set["parent_sku_id"] = $params['id'];
					}
					
					$dealers_db->insert("b_sku", $set);
				}
			}
			
			if(1 == $params['id_type'])
			{
				$this->_redirect("/clients/sku-details/product_id/".$params['id']."/msg/1");
			}else{
				$this->_redirect("/clients/sku-details/sku_id/".$params['id']."/msg/1");
			}
		}else{
			$this->_redirect("/clients/sku-mgt");
		}
	}
	
    function skuTreeAction()
    {
		$params = $this->_request->getParams();
		$this->view->title = "SKU总览";
		
		$mod_b_sku = new Db_Tables2_BSku();
		
		if($params['sku_id'])
		{
			$this->view->data = $mod_b_sku->GetSkuInfo($params['sku_id'], 2, 2);
//			$ext = new Sys_Extensions();
//			$ext->FormatArray($this->view->data);die;
			$this->view->sku_id = intval($params['sku_id']);
			$this->view->title = $this->view->data['self']['sku_name'];
		}else{
			echo "非法操作";
			die;
		}
		
		if($params['msg'])
		{
				switch ($params['msg'])
				{
					case 1:
						$this->view->msg = "更新成功。";
						break;
				}
		}
	}
	
	function skuTreeSubmitAction()
	{
		$params = $this->_request->getParams();
//		$ext = new Sys_Extensions();
//		$ext->FormatArray($params);
//		die;
		
		$ids = array();
		
		foreach($params as $pkey => $pval)
		{
			if("sku_name" == substr($pkey, 0, 8))
			{
				$ex = explode("_", $pkey);
				$ids[] = intval($ex[2]);
			}
		}
		
		if(!empty($ids))
		{
			$dealers_db = Zend_Registry::get('dealers_db');
			
			foreach($ids as $sku_id)
			{
				$set['sku_name'] = $params['sku_name_'.$sku_id];
				$set['unit_name'] = $params['unit_name_'.$sku_id];
				
				if(is_numeric($params['unit_price_'.$sku_id]))
				{
					$set['unit_price'] = $params['unit_price_'.$sku_id];
				}else{
					$set['unit_price'] = NULL;
				}
				
				$set['status'] = $params['status_'.$sku_id];
				
				$where = "sku_id = ".$sku_id;
				
				$dealers_db->update("b_sku", $set, $where);
			}
		}
		
		$this->_redirect("/clients/sku-tree/sku_id/".$params['sku_id']."/msg/1");
	}
	
    function bindSignerAction()
    {
		$params = $this->_request->getParams();
		
		$this->view->title = "关联签约公司";
		
		$mod_tables = new Db_Joins_TablesJoins();
    	
		$this->view->data = $mod_tables->GetClientSigner();
		
		if($params['status'])
		{
				switch ($params['status'])
				{
					case 1:
						$this->view->msg = "更改成功";
						break;
					case 2:
						$this->view->msg = "更改失败";
						break;
				}
		}
	}
}